package com.jingdianjichi.subject.application.mq.consumer;

import com.alibaba.fastjson.JSON;
import com.jingdianjichi.subject.domain.entity.SubjectLikedBO;
import com.jingdianjichi.subject.domain.service.SubjectLikedDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 点赞消息同步的监听器
 */
@Component
@RocketMQMessageListener(topic = "subject-liked", consumerGroup = "subject-liked-consumer")       //指定监听的话题和消费组就在注解中定义即可
@Slf4j
//实现监听器的接口,泛型是消息的类型
public class SubjectLikedConsumer implements RocketMQListener<String> {

    @Resource
    private SubjectLikedDomainService likedDomainService;

    //消费消息
    @Override
    public void onMessage(String message) {
        log.info("接受到了mq点赞的消息:{}",message);

        SubjectLikedBO likedBO= JSON.parseObject(message,SubjectLikedBO.class);

        //就是向数据库中同步
        likedDomainService.syncLikedByMessage(likedBO);
    }
}
